package com.chengxiaoxiao.ai_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chengxiaoxiao.ai_demo.api.InstructionInfoControllerApi;
import com.chengxiaoxiao.ai_demo.entity.InstructionInfo;
import com.chengxiaoxiao.ai_demo.service.InstructionInfoService;
import com.chengxiaoxiao.ai_demo.vo.PageResult;
import com.chengxiaoxiao.ai_demo.vo.Result;
import com.chengxiaoxiao.ai_demo.vo.query.InstructionInfoQuery;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 指令信息 接口控制器
 *
 * @Description:
 * @Author: Cheng Xiaoxiao
 * @Date: 2025-03-18 10:02:00
 */
@RestController
@RequestMapping("/instruction-info")
public class InstructionInfoController implements InstructionInfoControllerApi {
    @Resource
    private InstructionInfoService instructionInfoService;


    /**
     * 获取条件查询信息
     *
     * @param instructionInfoQuery 查询条件
     * @return 查询封装
     */
    private QueryWrapper<InstructionInfo> getInstructionInfoQueryWrapper(InstructionInfoQuery instructionInfoQuery) {
        QueryWrapper<InstructionInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(InstructionInfo::getCreateTime);
        wrapper.lambda().like(StringUtils.isNotBlank(instructionInfoQuery.getInstructionName()), InstructionInfo::getInstructionName, instructionInfoQuery.getInstructionName());
        wrapper.lambda().like(StringUtils.isNotBlank(instructionInfoQuery.getInstructionDescription()), InstructionInfo::getInstructionDescription, instructionInfoQuery.getInstructionDescription());
        wrapper.lambda().eq(StringUtils.isNotBlank(instructionInfoQuery.getInstructionType()), InstructionInfo::getInstructionType, instructionInfoQuery.getInstructionType());
        return wrapper;
    }

    /**
     * 按照条件查询指令信息
     *
     * @param instructionInfoQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<InstructionInfo>> list(InstructionInfoQuery instructionInfoQuery) {
        return Result.success(instructionInfoService.list(getInstructionInfoQueryWrapper(instructionInfoQuery)));
    }

    /**
     * 分页及条件查询指令信息
     *
     * @param page                 页码
     * @param size                 分页大小
     * @param instructionInfoQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<InstructionInfo>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, InstructionInfoQuery instructionInfoQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<InstructionInfo> pageParam = new Page<>(page, size);

        instructionInfoService.page(pageParam, getInstructionInfoQueryWrapper(instructionInfoQuery));
        PageResult<InstructionInfo> pageResult = new PageResult<>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取指令信息详情
     *
     * @param id 指令信息ID
     * @return 指令信息详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<InstructionInfo> detail(@PathVariable("id") String id) {
        return Result.success(instructionInfoService.detailById(id));
    }

    /**
     * 增加指令信息
     *
     * @param instructionInfo 指令信息增加实体
     * @return 增加后的指令信息
     */
    @Override
    @PostMapping
    public Result<InstructionInfo> add(@RequestBody InstructionInfo instructionInfo) {
        return Result.success(instructionInfoService.saveInstructionInfo(instructionInfo));
    }

    /**
     * 修改指令信息
     *
     * @param id              指令信息ID
     * @param instructionInfo 指令信息修改实体
     * @return 修改后的指令信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<InstructionInfo> updateById(@PathVariable("id") String id, @RequestBody InstructionInfo instructionInfo) {
        instructionInfo.setId(id);
        return Result.success(instructionInfoService.updateInstructionInfoById(instructionInfo));
    }


    /**
     * 删除指令信息
     *
     * @param id 指令信息ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        instructionInfoService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除指令信息
     *
     * @param idList 指令信息ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        instructionInfoService.removeByIds(idList);
        return Result.success(true);
    }
}
